package com.example.fp.zlz.controller;

import com.example.fp.cgc.code.logAnno.LogAnno;
import com.example.fp.zlz.entity.Admin;
import com.example.fp.zlz.entity.Userinfo;
import com.example.fp.zlz.mapper.AdminMapper;
import com.example.fp.zlz.mapper.UserinfoMapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import java.io.IOException;
import java.security.Principal;

@Controller
@RequestMapping
public class AdminWebController {
	@Autowired
	private AdminMapper adminMapper;
	@Autowired
	private UserinfoMapper userinfoMapper;

	@LogAnno(operateType = "查看用户信息")
	@ApiOperation("用户信息")
	@RequestMapping(value = "/userinfo/{uid}")
	public ModelAndView userInfo(@PathVariable int uid, Model model) {
		Admin admin = new Admin();
		admin.setId(uid);
		model.addAttribute("userinfo", adminMapper.selectuserinfo(admin));
		return new ModelAndView("userweb/userinfo", "userModel", model);
	}

	@LogAnno(operateType = "修改用户头像")
	@ApiOperation("修改头像")
	@PostMapping(value = "/file2")
	@ResponseBody
	public String uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("uid") int uid) throws IOException {
		Admin admin = new Admin();
		admin.setPic(file.getBytes());
		admin.setId(uid);
		adminMapper.updatepic(admin);
		return "修改成功,点击刷新";
	}

	@GetMapping("/modifyUser/{uid}")
	public ModelAndView modifyUser(@PathVariable("uid") int uid, Model model) {
		Admin admin = new Admin();
		admin.setId(uid);
		model.addAttribute("userinfo", adminMapper.selectuserinfo(admin));
		return new ModelAndView("userweb/userupdate", "userModel", model);
	}

	@PostMapping(value = "/updateuserinfo")
	public ModelAndView updateuserinfo(Userinfo userinfo) {
		userinfoMapper.updateuserinfobyid(userinfo);
		return new ModelAndView("redirect:/userinfo/" + userinfo.getId()); //重定向到list页面
	}

	@PostMapping(value = "/getuserinfobyid/{uid}")
	@ResponseBody
	public Userinfo getuserinfo(@PathVariable String uid) {
		return adminMapper.selectbyuid(Integer.parseInt(uid));
	}
	@GetMapping("/invalidsession")
	public ModelAndView invalidSession(Principal principal){
		adminMapper.lgoutsetsession(principal.getName());
		return new ModelAndView("redirect:/logout");
	}
	@GetMapping("/bgchange/{uid}&{bg}")
	@ResponseBody
	public String bgChange(@PathVariable("uid") String uid,@PathVariable("bg") String bg){

		Admin admin = new Admin();
		admin.setId(Integer.parseInt(uid));
		admin.setBg(Integer.parseInt(bg));
		adminMapper.updatebg(admin);
		return "修改成功,点击刷新";
	}
}
